﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DefineGenerated.DAL
{
    public static class IOHelp
    {
        /// <summary>
        /// 枚举文件夹下所有文件包含子目录
        /// </summary>
        /// <param name="dir">目标目录</param>
        /// <param name="list">结果</param>
        /// <param name="searchPattern">正则表达式,空字符表示不过滤</param>
        public static void Director(string dir, List<string> list, string searchPattern)
        {
            DirectoryInfo d = new DirectoryInfo(dir);
            FileInfo[] files = d.GetFiles();//文件
            DirectoryInfo[] directs = d.GetDirectories();//文件夹
            foreach (FileInfo f in files)
            {
                if (searchPattern == "")
                {
                    list.Add(f.FullName);//添加文件名到列表中  
                }
                if (Regex.IsMatch(Path.GetFileName(f.FullName), searchPattern))
                {
                    list.Add(f.FullName);//添加文件名到列表中  
                }
            }
            //获取子文件夹内的文件列表，递归遍历  
            foreach (DirectoryInfo dd in directs)
            {
                Director(dd.FullName, list, searchPattern);
            }
        }


    }
}
